package cn.itcast.web.controller.cargo;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.util.IdUtil;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.util.DownloadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/cargo/packing",name = "装箱管理")
public class PackingController extends BaseController {
    @Reference
    private PackingService packingService;

    @RequestMapping(value = "/list",name = "装箱管理分页查询")
    public String packingList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "6") Integer pageSize){
        PackingExample packingExample = new PackingExample();
        PackingExample.Criteria criteria = packingExample.createCriteria();
//        criteria.andStateEqualTo(0);
        PageInfo page = packingService.findByPage(pageNum, pageSize, packingExample);
        request.setAttribute("page",page);
        return "cargo/packing/packing-list";
    }

    @RequestMapping(value = "/edit",name = "保存装箱单或修改")
    public String toPacking(Packing packing){
        packing.setCreateBy(getLoginUser().getUserName());
        packing.setCreateDept(getLoginUser().getCreateDept());
        if(StringUtils.isBlank(packing.getPackingListId())){
            packingService.save(packing);
        }else{
            packingService.update(packing);
        }
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/submit",name = "提交")
    public String submit(String id){
        Packing packing = packingService.selectByPrimaryKey(id);
        packing.setState(1);
        packingService.update(packing);
        return "redirect:/cargo/packing/list.do";
    }

    @RequestMapping(value = "/cancel",name = "取消")
    public String cancel(String id){
        Packing packing = packingService.selectByPrimaryKey(id);
        packing.setState(0);
        packingService.update(packing);
        return "redirect:/cargo/packing/list.do";
    }


    @RequestMapping(value = "/toUpdate",name = "去修改页面")
    public String toUpdate(String id){
        Packing packing = packingService.selectByPrimaryKey(id);
        request.setAttribute("packing",packing);
        return "cargo/packing/packing-update";
    }

    @RequestMapping(value = "/delete",name = "删除")
    public String deleteById(String id){
      packingService.deleteByPrimaryKey(id);
      return "redirect:/cargo/packing/list.do";
    }


    @RequestMapping(value = "/generate",name = "委托单新增页面页面")
    public String generate(String id){
        request.setAttribute("id",id);
        return "cargo/shipping/shipping-toShipping";
    }

    @RequestMapping(value = "/exportExcel",name = "导出excel")
    public void exportExcel(String id) throws Exception {
        Packing packing = packingService.selectByPrimaryKey(id);

        String realPath = session.getServletContext().getRealPath("make/xlsprint/5-装箱单.xlsx");// 获取模板真实路径
        TemplateExportParams params = new TemplateExportParams(realPath);
        // 2.准备数据（模型）
        Map map = new HashedMap();
        map.put("title","装箱单表");
        map.put("packingListId", packing.getPackingListId());
        map.put("seller", packing.getSeller());
        map.put("buyer", packing.getBuyer());
        map.put("invoiceNo", packing.getInvoiceNo());
        map.put("invoiceDate", packing.getInvoiceDate());
        map.put("marks", packing.getMarks());
        map.put("descriptions", packing.getDescriptions());
        // 3.封装为workbook
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);

        // iii.使用工具类完成文件下载
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        workbook.write(byteArrayOutputStream);
        DownloadUtils.download(byteArrayOutputStream, response, "装箱单.xlsx");
    }

}
